import axios from "axios";
import Chrome from "@/Chrome";
const http = axios.create({
  baseURL: "https://turingbot.io/api/v1/", // url = base url + request url
  timeout: 20000,
  // withCredentials: true,
  headers: {
    "Content-Type": "application/json"
  }
});

http.interceptors.request.use(
  config => {
    // const token = Cookies.get("token");
    // if (token) {
    // 自定义请求头
    config.headers.Authorization = 'Bearer '+ localStorage.getItem('token');
    // }
    return config;
  },
  error => {
    console.log(error); // for debug    return Promise.reject(error);
  }
);

http.interceptors.response.use(
  response => {
    // console.log(response.data.error === 'email_is_required')
    if(response.data.error === 'email_is_required' && Chrome) {
     window.location.href = '/#/login'
    }
    return response.data;
  },
  error => {
    console.log("err" + error.message); // for debug
   
    // if(error.message === 'invalid signature') {
      localStorage.removeItem('token')
      window.location.href = '/#/login'
    // }
    // if (error.response.status === 403) {
    //   showWindowMessage(1, "登录已过期, 请重新登录", this);
    //   localStorage.clear();
    //   // @ts-ignore
    //   this.$route.push({
    //     path: "/login"
    //   });
    // }
    return Promise.reject(error);
  }
);
export { http };
